package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.Dept;
import com.originskyseed.platform.user.model.LevelSystemRelDept;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.ILevelSystemRelDeptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.LevelSystem;
import com.originskyseed.platform.user.service.ILevelSystemService;
import com.originskyseed.platform.user.vo.request.LevelSystemRequestVO;

/**
 * 等级体系信息Controller类（OSI_LEVEL_SYSTEM）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/levelSystem")
@BaseLocaleResource(localeFileName = "i18n_user")
public class LevelSystemController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(LevelSystemController.class);

	@DubboReference
	private ILevelSystemService levelSystemService;

	@DubboReference
	private ILevelSystemRelDeptService levelSystemRelDeptService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/levelSystemList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<LevelSystem>> levelSystemList(HttpServletRequest request) {
		BaseResponseBody<BasePage<LevelSystem>> result = null;
		try {
			// 请求对象
			LevelSystemRequestVO requestVO = this.getParameterBean(LevelSystemRequestVO.class);

			// 分页查询
			BasePage<LevelSystem> page = new BasePage<LevelSystem>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.levelSystemService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<LevelSystem>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<LevelSystem>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/levelSystemSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<LevelSystem> levelSystemSave(HttpServletRequest request) {
		BaseResponseBody<LevelSystem> result = null;
		try {
			// 当前登录用户
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			// 请求对象
			LevelSystem levelSystem = this.getParameterBean(LevelSystem.class);

			if (StringUtils.isNotBlank(levelSystem.getId())) {
				this.levelSystemService.updateByPrimaryKey(levelSystem);
			} else {
				levelSystem.setState(1);
				levelSystem.setIsDel(1);
				levelSystem.setCreatorId(userInfo.getId());
				levelSystem.setCreator(userInfo.getName());
				this.levelSystemService.insertSelective(levelSystem);
			}
			result = new BaseResponseBody<LevelSystem>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), levelSystem);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<LevelSystem>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/levelSystemDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> levelSystemDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			for (String id:ids) {
				if(StringUtils.isNotBlank(this.levelSystemRelDeptService.selectExistLevelSystem(id))){
					return  new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.BIND_DEPT_LEVEL_DELETE_ERROR.getMsg()),null);
				}
			}

			this.levelSystemService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}


	/**
	 * 查询列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectLevelSystemList.json", method = RequestMethod.POST)
	public BaseResponseBody selectLevelSystemList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			LevelSystem levelSystem = this.getParameterBean(LevelSystem.class);

			// 查询列表
			List<LevelSystem> levelSystemList = this.levelSystemService.selectList(levelSystem);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), levelSystemList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}


	/**
	 * 查询详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectLevelSystemById.json", method = RequestMethod.POST)
	public BaseResponseBody<BasePage<LevelSystem>> selectLevelSystemById(HttpServletRequest request) {
		BaseResponseBody<BasePage<LevelSystem>> result = null;
		try {
			// 请求对象
			String id = this.getParameterString("id",null);

			// 查询详情
			LevelSystem levelSystem = this.levelSystemService.selectByPrimaryKey(id);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), levelSystem);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}


	/**
	 * 查询树
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectLevelSystemTree.json", method = RequestMethod.POST)
	public BaseResponseBody selectLevelSystemTree(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			LevelSystem query = this.getParameterBean(LevelSystem.class);

			// 查询列表
			List<LevelSystem> levelSystemList = this.levelSystemService.selectList(query);
			JSONArray ary = new JSONArray();
			JSONObject obj;
			for (LevelSystem levelSystem : levelSystemList) {
				obj = new JSONObject();
				obj.put("id", levelSystem.getId());
				obj.put("name", levelSystem.getTitle());
				ary.add(obj);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), ary);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}



	/**
	 * 保存等级体系绑定分公司关系
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/saveLevelSystemDept.json", method = RequestMethod.POST)
	public BaseResponseBody saveLevelSystemDept(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 当前登录用户
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			// 请求对象
			LevelSystemRelDept levelSystemRelDept = this.getParameterBean(LevelSystemRelDept.class);

			this.levelSystemRelDeptService.deleteByDeptId(levelSystemRelDept.getDeptId());
			levelSystemRelDept.setCreatorId(userInfo.getId());
			levelSystemRelDept.setCreator(userInfo.getName());
			// 保存信息
			this.levelSystemRelDeptService.insertSelective(levelSystemRelDept);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}